/**
 * jTable Jquery plugin
 *
 * @author KeiMOon
 * @version 1.0.0 (Feb 27 2010)
 * @requires Jquery 1.2.6 and laters
 *
 * Dual licensed under the MIT and LGPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/lgpl.html
 */

;(function($){
    $.jTableLoad = function(t, options) {
        var settings = $.extend({
            url : false,
            type : "post",
            errorMessage : "No data",
            params : false,
            onClickTwice : false,
            onSuccess : false
        }, options||{});
        if (settings.url == false) {
            return false;
        }
        
        var jtable = {
            init : function() {
                this.bindEvent();
                this.loadData();
            },

            loadData : function() {
                var send = null;
                if (settings.params) {
                    send = $.param(settings.params);
                }

                $.ajax({
                    url : settings.url,
                    type : settings.type,
                    dataType : 'html',
                    data : send,
                    success : function(data) {
                        $(t).find('tbody').html(data);
                        jtable.bindEvent();
                        if (settings.onSuccess) {
                            settings.onSuccess();
                        }
                    },
                    error : function() {
                        var colspan = $(t).find('thead th').length;
                        $(t).find('tbody').html('<tr><td colspan="' + colspan + '">' + settings.errorMessage + '<td></tr>');
                    }
                });
            },

            bindEvent : function() {
                $(t).find('tbody td.selectable').each(function(){
                    this.onselectstart = function() {
                        return false;
                    }
                })
                $(t).find('tbody td.selectable').click(function(e){
                    if (e.shiftKey) {
                        $(this).parent().toggleClass('selected');
                    }
                    else {
                        var selected = $(this).parent().hasClass('selected');
                        $(t).find('tbody tr').removeClass('selected');
                        if (selected && settings.onSelectTwice) {
                            $(this).unbind('click');
                            settings.onSelectTwice(this);
                        }
                        else {
                            $(this).parent().addClass('selected');
                        }
                    }
                    return false;
                });
            },
            
            bindEventFor : function(cell) {
                cell.onselectstart = function() {
                    return false;
                }
                $(cell).click(function(e){
                    if (e.shiftKey) {
                        $(this).parent().toggleClass('selected');
                    }
                    else {
                        var selected = $(this).parent().hasClass('selected');
                        $(t).find('tbody tr').removeClass('selected');
                        if (selected && settings.onSelectTwice) {
                            $(this).unbind('click');
                            settings.onSelectTwice(this);
                        }
                        else {
                            $(this).parent().addClass('selected');
                        }
                    }
                    return false;
                });
            }
        }

        t.settings = settings;
        t.jtable = jtable;
        jtable.init();
    };

    $.fn.jTable = function(options) {
        return this.each(function(){
            $.jTableLoad(this, options);
        });
    };

    $.fn.jTableBindEventFor = function(cell) {
        return $(this).each(function(){
            if (this.jtable) {
                this.jtable.bindEventFor(cell);
            }
        });
    }

})(jQuery);